package leetcode.dataStructure;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

 class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Tree {
    @Test
    public void preorderTraversal() {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        List<Integer> list = new ArrayList<>();
        pre(root,list);
        System.out.println(list);
    }


    // 返回条件就是走到null，
    public void pre(TreeNode root, List<Integer> list) {
        if(root==null){
            return ;
        }
        list.add(root.val);

        pre(root.left, list);
        pre(root.right,list);
    }
}